package com.chinanet;

import com.chinanet.config.FileStorageProperties;
import com.chinanet.listener.ApplicationStartup;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.boot.context.ApplicationPidFileWriter;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.context.WebServerPortFileWriter;
import org.springframework.data.jpa.convert.threeten.Jsr310JpaConverters;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
import org.springframework.hateoas.config.EnableHypermediaSupport;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;

import javax.annotation.PostConstruct;
import java.util.TimeZone;

@EntityScan(basePackageClasses = {
        Application.class,
        Jsr310JpaConverters.class
})

/*
 * @SpringBootApplication注解是SpringBoot的核心注解，它其实是一个组合注解：
 * 该注解主要组合了以下注解：
 * 1.@SpringBootConfiguration：这是SpringBoot项目的配置注解，这也是一个组合注解：
 * 在Spring Boot项目中推荐使用@SpringBootConfiguration替代@Configuration
 * 2.@EnableAutoConfiguration：启用自动配置，该注解会使SpringBoot根据项目中依赖的jar包自动配置项目的配置项：
 * 3.@ComponentScan：默认扫描@SpringBootApplication所在类的同级目录以及它的子目录。
 */
@SpringBootApplication
@EnableJpaAuditing(auditorAwareRef = "auditorAware")
// 开启Quartz
@EnableScheduling
@EnableConfigurationProperties({
        FileStorageProperties.class
})
// 开启异步操作
@EnableAsync
//@EnableCaching
@EnableHypermediaSupport(type = EnableHypermediaSupport.HypermediaType.HAL)
public class Application {


    // 使用默认时区
    @PostConstruct
    void init() {
        TimeZone.setDefault(TimeZone.getDefault());
    }

    public static void main(String[] args) {
        SpringApplication springApplication = new SpringApplication(Application.class);
        springApplication.addListeners(new ApplicationStartup());
        /** 使用脚本方式关闭应用程序，将应用程序上下文的PID写入到文件
         /* 将以下内容写成.bat或者是bash脚本
         /* kill $(cat ./shutdown.pid)
         **/
        springApplication.addListeners(new ApplicationPidFileWriter("./shutdown.pid"));
        springApplication.addListeners(new WebServerPortFileWriter("./port.txt"));
        springApplication.run(args);
    }
}
